using System;
using SharpMud.Net.Telnet.IAC;

namespace SharpMud.Net.Telnet.Session.NegotationAgents
{
	/// <summary>
	/// This is a flawed, flailing attempt at trying to 'negotiate' the use of Telnet Go-Ahead. I dunno
	/// if this is even negotiable. Worth a try.
	/// </summary>
	public class GoAhead
	{
		private TelnetSessionBase _HostSession;

		#region Constructors
		private GoAhead()
		{
		}
		public GoAhead(TelnetSessionBase hostSession)
		{
			//_Behavior = (FeatureNegotiationBehavior)(-1);

			_Enabled = true;
			_ChangesPending = 0;
			_HostSession = hostSession;
			_RequireClientVerification = false;
			hostSession.IAC.In.IAC_Feature_Negotiation +=new SharpMud.Net.Telnet.Session.DataProcessors.IAC.IACInputEventHandler(IAC_Feature_Negotiation);
		}

		private bool _Enabled;
		public bool Enabled
		{
			get
			{
				return _Enabled;
			}
			set
			{
				if(value!=_Enabled)
				{
					if(_Enabled==true && value==false && (ChangePending==false || RequireClientVerification==false))
					{
						//Need to turn remote Go-Ahead support off
						_ChangesPending++;
						this._HostSession.IAC.Out.Send_FeatureNegotiation_Packet(ControlCodes.WONT,ControlCodes.GA);
						if(!RequireClientVerification)
						{
							_Enabled = false;
						}
						return;
					}
					if(_Enabled==false && value==true && (ChangePending==false || RequireClientVerification==false))
					{
						//Need to turn remote Go-Ahead support back on
						_ChangesPending++;
						this._HostSession.IAC.Out.Send_FeatureNegotiation_Packet(ControlCodes.WILL,ControlCodes.GA);
						if(!RequireClientVerification)
						{
							_Enabled = true;
						}
						return;
					}
				}
			}
		}
		private int _ChangesPending;
		public bool ChangePending
		{
			get
			{
				return (_ChangesPending>0);
			}
		}

		private bool _RequireClientVerification;
		public bool RequireClientVerification
		{
			get
			{
				return _RequireClientVerification;
			}
			set
			{
				_RequireClientVerification = value;
			}
		}
		#endregion

		private void IAC_Feature_Negotiation(SharpMud.Net.Telnet.Session.DataProcessors.IAC.InputLayer sender, SharpMud.Net.Telnet.Session.DataProcessors.IAC.IACInputEventArgs e)
		{
			if(e.FeatureType!=ControlCodes.GA) return;
			
			if(e.NegotiationQuery == ControlCodes.WILL)
			{
				//The client is saying it wants us to accept its Go-Aheads.
				//Servers don't do that. Tell it NO! Bad client!
				_HostSession.IAC.Out.Send_FeatureNegotiation_Packet(ControlCodes.DONT,ControlCodes.GA);
				return;
			}
			if(e.NegotiationQuery == ControlCodes.WONT)
			{
				//The client is saying it doesn't want us to accept its Go-Aheads.
				//Which is okay, since it isn't supported server side.
				return;
			}
			if(e.NegotiationQuery == ControlCodes.DO)
			{
				if(!Enabled)
					_Enabled = true;
				if(ChangePending)
					_ChangesPending--;
				return;
			}
			if(e.NegotiationQuery == ControlCodes.DONT)
			{
				if(Enabled)
					_Enabled = false;
				if(ChangePending)
					_ChangesPending--;
				return;
			}
		}
	}
}